package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	
	/**
	 * 路径素描示例
	 * @author StefanieKaka
	 */
	public final class PathSketch extends Sprite 
	{
		/// 构造函数
		public function PathSketch() 
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
		}
		
		/// 鼠标按下事件
		private function onMouseDownHandler(evt : MouseEvent) : void
		{
			m_commands.push(GraphicsPathCommand.MOVE_TO);
			m_data.push(this.mouseX, this.mouseY);
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			
			draw();
		}
		
		/// 鼠标移动事件
		private function onMouseMoveHandler(evt : MouseEvent) : void
		{
			m_commands.push(GraphicsPathCommand.LINE_TO);
			m_data.push(this.mouseX, this.mouseY);
			
			draw();
		}
		
		/// 鼠标抬起事件
		private function onMouseUpHandler(evt : MouseEvent) : void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
		}
		
		/// 按键按下事件
		private function onKeyDownHandler(evt : KeyboardEvent) : void
		{
			if (evt.keyCode == Keyboard.SPACE)
				m_lineColor = Math.random() * 0xffffff;
				
			if (evt.keyCode == Keyboard.UP)
				++m_lineWidth;
				
			if (evt.keyCode == Keyboard.DOWN)
				m_lineWidth = Math.max(0, m_lineWidth - 1);
				
			draw();
		}
		
		/// 执行绘图命令
		private function draw() : void
		{
			this.graphics.clear();
			this.graphics.lineStyle(m_lineWidth, m_lineColor);
			this.graphics.drawPath(m_commands, m_data);
		}
		
		private var m_commands : Vector.<int> = new Vector.<int>();
		private var m_data : Vector.<Number> = new Vector.<Number>();
		private var m_lineColor : uint = 0;
		private var m_lineWidth : Number = 0;
		
	}

}